/*
 * @Description: 
 * @Autor: wangwangwang
 * @Date: 2021-01-28 09:46:13
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-24 15:21:56
 */
import axios from 'axios';
import {
  getToken,
  getUserId
} from '@/utils/auth';
exports.install = function (Vue) {
  Vue.prototype.$Download = function (url, data) { //全局函数
    const loading = this.$loading({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
    });
    let baseURL = ''
    if (process.env.NODE_ENV == 'development') {
      baseURL = '/dev'
    } else if (process.env.VUE_APP_API_HOST) {
      baseURL = process.env.VUE_APP_API_HOST
    }
    axios({
      method: 'post',
      url: baseURL + url, // 请求地址
      headers: {
        'access-token': getToken(),
        'user-id': getUserId()
      },
      data: {
        page: 1,
        pageSize: 999999,
        ...data
      }, // 参数
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(res => {
      loading.close();
      let json = JSON.parse(JSON.stringify(res.headers))//头部数据
      const blob = new Blob([res.data]);
      let link = document.createElement('a');
      link.href = window.URL.createObjectURL(blob);
      if (url == '/base/system/user/exportTemplate') {
        link.download = '用户导入模板.xls';
      } else if (url == '/base/system/user/exportPersonalTemplate') {
        link.download = '个人用户导入模板.xls';
      } else if (url == '/base/system/user/export') {
        link.download = '用户导出.xls';
      } else if (url == '/icps/system/sign/basics/graphic/exportTemplate') {
        link.download = '浇筑部位导入模板.xls';
      } else {
        let name = ''
        if (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) {
          name = json['content-disposition'];
        } else {
          name = decodeURIComponent(escape(json['content-disposition']))
        }
        link.download = name
      }
      link.click();
      window.URL.revokeObjectURL(link.href);
    });
  }
  Vue.prototype.$fromatTable = function (List, data, id, name) {
    for (let i = 0; i < List.length; i++) {
      if (List[i][id] === data) {
        return List[i][name]
      }
    }
    return '无数据'
  }
  Vue.prototype.$wps = function (fileUrl, waterMark) {
    let baseURL = ''
    if (process.env.NODE_ENV == 'development') {
      baseURL = '/dev'
    }
    axios({
      method: 'get',
      url: baseURL + '/icps/outer/sign/outerApi/wps/getViewUrlWebPath', // 请求地址
      headers: {
        'access-token': getToken(),
        'user-id': getUserId()
      },
      params: {
        fileUrl: fileUrl,
        waterMark: waterMark
      }, // 参数
    }).then(res => {
      sessionStorage.wpsUrl = res.data.data;
      sessionStorage.token = getToken();
      // sessionStorage.wpsUrl = 'https://wwo.wps.cn/office/w/046b614109334c0e802c9e2ce1258a7a?_w_userid=-1&_w_filetype=web&_w_filepath=http://182.150.57.78:8060/生产调度-接口文档 -13.docx&_w_tokentype=1&_w_appid=5b8f173bd752464d81b7aa78001c697f&_w_redirectkey=123456&_w_signature=PqTmHUHcdG5RjH1OSgmCvBBe%2FXQ%3D';
      // sessionStorage.token = '046b614109334c0e802c9e2ce1258a7a';
      const jump = this.$router.resolve({ name: 'wps' })
      window.open(jump.href, '_blank');
    })
  }
};